package com.qujia.station.controller;

import com.qujia.common.annotation.Log;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.station.domain.Station;
import com.qujia.station.domain.StationRecharge;
import com.qujia.station.domain.StationRechargeVo;
import com.qujia.station.service.IStationRechargeService;
import com.qujia.station.service.IStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 油站充值Controller
 * 
 * @author qujia
 * @date 2022-04-17
 */
@RestController
@RequestMapping("/station/recharge")
public class StationRechargeController extends BaseController
{
    @Autowired
    private IStationRechargeService stationRechargeService;

    @Autowired
    private IStationService stationService;
    /**
     * 查询油站充值列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationRechargeVo stationRecharge)
    {
        startPage();
        LoginUser loginUser= SecurityUtils.getLoginUser();
        stationRecharge.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        List<StationRechargeVo> list = stationRechargeService.selectStationRechargeVoList(stationRecharge);
        return getDataTable(list);
    }

    /**
     * 导出油站充值列表
     */
    @Log(title = "油站充值", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationRechargeVo stationRecharge)
    {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        stationRecharge.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        List<StationRechargeVo> list = stationRechargeService.selectStationRechargeVoList(stationRecharge);
        ExcelUtil<StationRechargeVo> util = new ExcelUtil<StationRechargeVo>(StationRechargeVo.class);
        return util.exportExcel(list, "recharge");
    }

    /**
     * 油站充值余额
     */
    @GetMapping(value = "/{stationId}")
    public AjaxResult getInfo(@PathVariable("stationId") Long stationId)
    {
        return AjaxResult.success(stationRechargeService.selectStationRechargeById(stationId));
    }

    /**
     * 新增油站充值
     */
    @Log(title = "油站充值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationRecharge stationRecharge)
    {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        stationRecharge.setEnterpriseId(loginUser.getUser().getEnterpriseId());
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat fmtAll = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = fmt.format(new Date());
        try {
            Date parse = fmtAll.parse(format + " 23:59:59");
            stationRecharge.setRechargeEndTime(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        stationRecharge.setRechargeTime(new Date());
        stationRecharge.setUserId(loginUser.getUser().getUserId());

        //更新油站修改时间
        Station station = new Station();
        station.setStationId(stationRecharge.getStationId());
        stationService.updateStation(station);
        return toAjax(stationRechargeService.insertStationRecharge(stationRecharge));
    }

    /**
     * 修改油站充值
     */
    @PreAuthorize("@ss.hasPermi('station:recharge:edit')")
    @Log(title = "油站充值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationRecharge stationRecharge)
    {
        return toAjax(stationRechargeService.updateStationRecharge(stationRecharge));
    }

    /**
     * 删除油站充值
     */
    @PreAuthorize("@ss.hasPermi('station:recharge:remove')")
    @Log(title = "油站充值", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rechargeIds}")
    public AjaxResult remove(@PathVariable Long[] rechargeIds)
    {
        return toAjax(stationRechargeService.deleteStationRechargeByIds(rechargeIds));
    }
}
